# Contributor: psykose <alice@ayaya.dev>
# Contributor: Alex Yam <alex@alexyam.com>
# Maintainer: psykose <alice@ayaya.dev>
pkgname=wasi-libcxx
# match llvm version
pkgver=14.0.6
_llvmver="${pkgver%%.*}"
_wasi_sdk_ver=16
pkgrel=0
pkgdesc="WASI LLVM C++ standard library"
subpackages="wasi-libcxxabi:_libcxxabi"
url="https://libcxx.llvm.org/"
arch="all"
license="Apache-2.0 WITH LLVM-exception"
makedepends="
	clang
	cmake
	libxml2-dev
	llvm$_llvmver-dev
	python3-dev
	samurai
	wasi-libc
	zlib-dev
	"
source="https://github.com/llvm/llvm-project/releases/download/llvmorg-$pkgver/libcxx-$pkgver.src.tar.xz
	https://github.com/llvm/llvm-project/releases/download/llvmorg-$pkgver/libcxxabi-$pkgver.src.tar.xz
	https://github.com/llvm/llvm-project/releases/download/llvmorg-$pkgver/llvm-$pkgver.src.tar.xz
	https://github.com/WebAssembly/wasi-sdk/archive/refs/tags/wasi-sdk-$_wasi_sdk_ver.tar.gz
	"
builddir="$srcdir"/wasi-libcxx
# TODO: check, needs to somehow pass wasi sysroot include to lit..
options="!check"

prepare() {
	default_prepare
	mkdir -p "$builddir"
	for s in libcxx libcxxabi llvm; do
		mv "$srcdir"/$s-$pkgver.src "$builddir"/$s
	done
	mv "$srcdir"/wasi-sdk-wasi-sdk-$_wasi_sdk_ver/wasi-sdk.cmake "$builddir"
	mv "$srcdir"/wasi-sdk-wasi-sdk-$_wasi_sdk_ver/cmake "$builddir"
	mv "$srcdir"/cmake/* "$builddir"/cmake
}

build() {
	export CFLAGS="$CFLAGS -fno-exceptions --sysroot=/usr/share/wasi-sysroot"
	export CXXFLAGS="$CXXFLAGS -fno-exceptions --sysroot=/usr/share/wasi-sysroot"

	cmake -B build-libcxx -G Ninja \
		-DCMAKE_BUILD_TYPE=MinSizeRel \
		-DCMAKE_MODULE_PATH="$builddir"/cmake \
		-DCMAKE_TOOLCHAIN_FILE="$builddir"/wasi-sdk.cmake \
		-DCMAKE_CXX_COMPILER_WORKS=ON \
		-DCMAKE_C_COMPILER_WORKS=ON \
		-DCMAKE_STAGING_PREFIX=/usr/share/wasi-sysroot \
		-DLIBCXX_ABI_VERSION=2 \
		-DLIBCXX_BUILD_EXTERNAL_THREAD_LIBRARY=OFF \
		-DLIBCXX_CXX_ABI=libcxxabi \
		-DLIBCXX_CXX_ABI_INCLUDE_PATHS=libcxxabi/include \
		-DLIBCXX_ENABLE_EXCEPTIONS=OFF \
		-DLIBCXX_ENABLE_EXPERIMENTAL_LIBRARY=OFF \
		-DLIBCXX_ENABLE_FILESYSTEM=OFF \
		-DLIBCXX_ENABLE_SHARED=OFF \
		-DLIBCXX_ENABLE_THREADS=OFF \
		-DLIBCXX_HAS_EXTERNAL_THREAD_API=OFF \
		-DLIBCXX_HAS_MUSL_LIBC=ON \
		-DLIBCXX_HAS_PTHREAD_API=OFF \
		-DLIBCXX_HAS_WIN32_THREAD_API=OFF \
		-DLIBCXX_INCLUDE_TESTS="$(want_check && echo ON || echo OFF)" \
		-DLIBCXX_LIBDIR_SUFFIX=/wasm32-wasi \
		-DLIBCXX_STANDALONE_BUILD=ON \
		-DWASI_SDK_PREFIX=/usr \
		libcxx
	cmake --build build-libcxx

	cmake -B build-libcxxabi -G Ninja \
		-DCMAKE_BUILD_TYPE=MinSizeRel \
		-DCMAKE_MODULE_PATH="$builddir"/cmake \
		-DCMAKE_TOOLCHAIN_FILE="$builddir"/wasi-sdk.cmake \
		-DCMAKE_CXX_COMPILER_WORKS=ON \
		-DCMAKE_C_COMPILER_WORKS=ON \
		-DCMAKE_STAGING_PREFIX=/usr/share/wasi-sysroot \
		-DCXX_SUPPORTS_CXX11=ON \
		-DLIBCXXABI_BUILD_EXTERNAL_THREAD_LIBRARY=OFF \
		-DLIBCXXABI_ENABLE_EXCEPTIONS=OFF \
		-DLIBCXXABI_ENABLE_PIC=OFF \
		-DLIBCXXABI_ENABLE_SHARED=OFF \
		-DLIBCXXABI_ENABLE_THREADS=OFF \
		-DLIBCXXABI_HAS_EXTERNAL_THREAD_API=OFF \
		-DLIBCXXABI_HAS_PTHREAD_API=OFF \
		-DLIBCXXABI_HAS_WIN32_THREAD_API=OFF \
		-DLIBCXXABI_INCLUDE_TESTS="$(want_check && echo ON || echo OFF)" \
		-DLIBCXXABI_LIBCXX_INCLUDES="$builddir"/build-libcxx/include/c++/v1 \
		-DLIBCXXABI_LIBCXX_PATH=libcxx \
		-DLIBCXXABI_LIBDIR_SUFFIX=/wasm32-wasi \
		-DLIBCXXABI_SILENT_TERMINATE:BOOL=ON \
		-DLIBCXXABI_STANDALONE_BUILD=ON \
		-DUNIX=ON \
		-DWASI_SDK_PREFIX=/usr \
		libcxxabi
	cmake --build build-libcxxabi
}

package() {
	DESTDIR="$pkgdir" cmake --install build-libcxx
}

_libcxxabi() {
	pkgdesc="WASI Low level support for the LLVM C++ standard library."
	DESTDIR="$subpkgdir" cmake --install "$builddir"/build-libcxxabi
}

sha512sums="
c39bb01317e8ae7e5b23a677754a866953b6c45188c194d3d76fbd276de9565df8bcbcab8cf6606584af7dd30a61f39376c0e2de6b8c32c69f7fc592587e710d  libcxx-14.0.6.src.tar.xz
f61926ce9c8bf4d704d9ee03a795ebd23730099d56c385e3cbd1f8b588b337c1394d6548f55e1452df5040117a80765c977ba256547d53cd21895e91ef0fffa6  libcxxabi-14.0.6.src.tar.xz
6461bdde27aac17fa44c3e99a85ec47ffb181d0d4e5c3ef1c4286a59583e3b0c51af3c8081a300f45b99524340773a3011380059e3b3a571c3b0a8733e96fc1d  llvm-14.0.6.src.tar.xz
501467cb04ee85ab2ccc3d8ab1beb5dd8957ca71cc51c86fd357991ddccb1a8c2656e24b947ea3a5acfaafd8c762f5ba20458c22b58a5a5c85ef8ecb7a76db65  wasi-sdk-16.tar.gz
"
